﻿using SharePointUtilitiesForDevelopers.Mapper;
using SharePointUtilitiesForDevelopers.Service;
using SharePointUtilitiesForDevelopers.SiteDataService;

namespace SharePointUtilitiesForDevelopers.Gateway
{
    /// <summary>
    /// Provides a gateway to the SharePoint site data service.
    /// </summary>
    public class SharePointSiteDataServiceGateway : ISharePointSiteDataServiceGateway
    {

        /// <summary>
        /// Gets the web.
        /// </summary>
        /// <typeparam name="TDomainObject">The type of the domain object.</typeparam>
        /// <param name="siteUrl">The URL.</param>
        /// <param name="mapper">The mapper.</param>
        /// <returns>A <see cref="TDomainObject"/>.</returns>
        public TDomainObject GetWeb<TDomainObject>(
            string siteUrl, 
            IMapper<SharePointSiteData, TDomainObject> mapper)
        {
            //  Get the proxy
            var client = SharePointServiceClientFactory.CreateInstance<SiteDataSoapClient, SiteDataSoap>(siteUrl);

            // Create out vars
            _sWebMetadata webMetaData;
            _sWebWithTime[] arrWebWithTime;
            _sListWithTime[] arrListWithTime;
            _sFPUrl[] arrUrls;
            string roles;
            ArrayOfString roleUsers;
            ArrayOfString roleGroups;

            // Get information about the web
            client.GetWeb(
                out webMetaData,
                out arrWebWithTime,
                out arrListWithTime,
                out arrUrls,
                out roles,
                out roleUsers,
                out roleGroups);

            // Create a domain object to hold site data
            var siteData = new SharePointSiteData
            {
                Title = webMetaData.Title,
                Url = siteUrl
            };

            // Return the mapped object
            return mapper.Map(siteData);

        }

    }
}